package com.thtfpc.demo;

import com.thtfpc.discovery.ScreenPlayer;
import com.thtfpc.utility.InputUtil;

import java.io.IOException;
import java.util.logging.Logger;

/*
投屏幕的服务端demo，该代码模拟uos系统监听局域网内请求播放屏幕的请求
 */
public class DemoForPlayer implements ScreenPlayer.RenderRequestListener {

    static Logger logger = Logger.getLogger(DemoForPlayer.class.getSimpleName());
    /*
    播放端对象
     */
    ScreenPlayer screenPlayer;

    public DemoForPlayer() {
        screenPlayer = new ScreenPlayer(this);
    }

    public void start() {
        try {
            //启动播放端监控
            screenPlayer.start();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void stop() {
        try {
            screenPlayer.stop();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /*
    当有客户请求播放
    return 0  代表同意播放要求
    return != 0 代表错误，无法相应播放请求
    可在本事件中对播放请求前进行截获，执行是否播放的判定
     */
    @Override
    public int onRequestRender(ScreenPlayer.RenderRequest renderRequest) {
        logger.info("[客户请求播放]onRequestRender:" + renderRequest);
        try {
            Process runtime = Runtime.getRuntime().exec("ffplay -i " + renderRequest.mediaURL);
            return 0;
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return 1;
    }

    public static void main(String[] args) throws IOException {
        DemoForPlayer demo = new DemoForPlayer();
        demo.start();
        System.out.println("type \"exit\" for quit.");
        InputUtil.quitFor("exit", new InputUtil.InputListener() {
            @Override
            public void onUserTypeString(String input) {
                System.out.println("type \"exit\" for quit.");
            }
        });
        demo.stop();
    }
}
